package com.sky.controller.user;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Reader;

@RestController
@RequestMapping("/user/order")
@Api(tags = "订单相关接口")
@Slf4j
public class OrderController {

    //注入OrderService对象
    @Autowired
    private OrderService orderService;

    /**
     * 提交订单
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation(value = "提交用户订单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("提交用户订单：{}",ordersSubmitDTO);

        //调用service提交订单
        OrderSubmitVO orderSubmitVO = orderService.submit(ordersSubmitDTO);

        //返回结果给前端
        return Result.success(orderSubmitVO);
    }

//    /**
//     * 订单支付
//     *
//     * @param ordersPaymentDTO
//     * @return
//     */
//    @PutMapping("/payment")
//    @ApiOperation("订单支付")
//    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
//        log.info("订单支付：{}", ordersPaymentDTO);
//        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
//        log.info("生成预支付交易单：{}", orderPaymentVO);
//        return Result.success(orderPaymentVO);
//    }

    /**
     * 模拟订单支付
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        // 模拟支付成功，更新数据库订单状态 -此时没有回调
        orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
        return Result.success(orderPaymentVO);
    }

    /**
     * 历史订单查询
     * @return
     */
    @ApiOperation(value = "历史订单查询")
    @GetMapping("/historyOrders")
    public Result<PageResult> page(int page,int pageSize,Integer status){
        log.info("历史订单查询,{},{},{}",page,pageSize,status);

        //调用service查询
        PageResult pageResult = orderService.page(page,pageSize,status);

        //返回结果给前端
        return Result.success(pageResult);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @ApiOperation(value = "订单详情查询")
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> details(@PathVariable("id") Long id){
        log.info("订单详情查询：{}",id);

        //调用service查询
        OrderVO orderVO = orderService.details(id);

        //返回结果给前端
        return Result.success(orderVO);
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @ApiOperation(value = "取消订单")
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable("id") Long id){
        log.info("取消订单：{}",id);

        //调用service取消订单
        orderService.userCancelById(id);

        //返回结果给前端
        return Result.success();
    }

    /**
     * 再来一单：重新将商品添加到购物车
     * @param id
     * @return
     */
    @ApiOperation(value = "再来一单")
    @PostMapping("repetition/{id}")
    public Result repetition(@PathVariable("id") Long id){
        log.info("再来一单：{}",id);

        //调用service将商品添加到购物车
        orderService.repetition(id);

        //返回结果给前端
        return Result.success();
    }

    /**
     * 客户催单
     * @param id
     * @return
     */
    @ApiOperation(value = "客户催单")
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id){

        orderService.reminder(id);

        return Result.success();
    }

}
